﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
namespace PewPew
{
    class Game
    {
        //public static int TARGET_RADIUS = 100;   
        private Random randomizer;
        private Canvas canvas;
        private Label enemyLabel;
        private Target currTarget;
        public Ellipse currHitPoint;
        public bool enemyHit = false;
        public int score = 0;

        public Target getNextTarget(double sizeFactor)
        {
            System.Drawing.Point center = generateNewCoordinates(sizeFactor);
            Target target = new Target(center, enemyLabel, sizeFactor);
            this.currTarget = target;
            this.enemyHit = false;
            return this.currTarget;
            
        }

        private System.Drawing.Point generateNewCoordinates(double sizeFactor)
        {
            int desiredRadius = (int) (sizeFactor * 0.1);
            int x = randomizer.Next(desiredRadius, (int)canvas.ActualWidth - desiredRadius);
            int y = randomizer.Next(desiredRadius, (int)canvas.ActualHeight - desiredRadius);

            return new System.Drawing.Point(x, y);
        }

        public Game(Canvas canvas, Label enemyLabel)
        {
            randomizer = new Random();
            this.canvas = canvas;
            this.enemyLabel = enemyLabel;
            this.currHitPoint = null;
        }

        public Target getCurrTarget() 
        {
            return this.currTarget;
        }

        public Ellipse getCurrHit()
        {
            return this.currHitPoint;
        }


        public bool isHit(Target tgt, System.Drawing.Point pnt, String command, bool targetAppears)
        {
            if (tgt != null)
            {
                return (targetAppears && (tgt.getName() == command) && tgt.ContainsPoint(pnt));
            }
            return false;
        }

        public void DrawHitPoint(System.Drawing.Point pnt, System.Windows.Media.SolidColorBrush color)
        {
            
            // remove old hit point
            if (this.currHitPoint != null)
            {
                canvas.Children.Remove(this.currHitPoint);
            }

            // generate new hitpoint
            Ellipse ellipse = new Ellipse();
            if (currTarget == null)
            {
                ellipse.Stroke = System.Windows.Media.Brushes.Red;
                ellipse.Fill = System.Windows.Media.Brushes.Red;
            }
            else
            {
                ellipse.Stroke = System.Windows.Media.Brushes.Black;
                ellipse.Fill = color;
            }

            ellipse.HorizontalAlignment = HorizontalAlignment.Center;
            ellipse.VerticalAlignment = VerticalAlignment.Center;
            if (this.getCurrTarget() != null)
            {
                ellipse.Width = (int)(2 * 0.1 * this.getCurrTarget().getRadius());
                ellipse.Height = (int)(2 * 0.1 * this.getCurrTarget().getRadius());
            }
            else 
            {
                ellipse.Width = (int)(2 * 0.1 * 100);
                ellipse.Height = (int)(2 * 0.1 * 100);
            }

            double left = pnt.X - (ellipse.Width / 2);
            double top = pnt.Y - (ellipse.Height / 2);
            ellipse.Margin = new Thickness(left, top, 0, 0);

            canvas.Children.Add(ellipse);
            this.currHitPoint = ellipse;

        }

        

    
    }
}
